<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Rule extends Modules_Core_Models_Rule
{
	public function reset($objectType, $objectId, $module)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'core_rule 
					SET allow = "0" 
					WHERE object_type = "' . $this->_conn->escape($objectType) . '"
						AND object_id = "' . $this->_conn->escape($objectId) . '"
						AND module_id = "' . $this->_conn->escape($module) . '"';
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
	
	public function add($rule)
	{
		$ruleId = $this->_conn->insert($rule, $this->_conn->_tablePrefix . 'core_rule');
		return $ruleId;	
	}
	
	public function deleteByRole($roleId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_rule 
							WHERE object_type = "role" AND object_id = "' . $this->_conn->escape($roleId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function update($rule)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'core_rule 
					SET module_id = "%s", controller_id = "%s", action_id = "%s", 
						object_type = "%s", object_id = "%s", allow = "%s" 
					WHERE rule_id = "%d"';
		$sql = sprintf($sql, $this->_conn->escape($rule['module_id']),
							$this->_conn->escape($rule['controller_id']),
							$this->_conn->escape($rule['action_id']),
							$this->_conn->escape($rule['object_type']),
							$this->_conn->escape($rule['object_id']),
							$this->_conn->escape($rule['allow']),
							$this->_conn->escape($rule['rule_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function getAclRules()
	{
		$sql  = "SELECT CONCAT(ru.object_type, '_', ru.object_id) AS role_name, ru.allow, ru.controller_id AS module_controller, NULL AS action_id
				FROM " . $this->_conn->_tablePrefix . "core_rule AS ru
				WHERE ru.action_id IS NULL
				
				UNION 
				
				SELECT CONCAT(ru.object_type, '_', ru.object_id) AS role_name, ru.allow, CONCAT(p.module_id, ':', p.controller_id) AS resource_name_2, p.action_id AS action_name 
				FROM " . $this->_conn->_tablePrefix . "core_rule AS ru
				INNER JOIN " . $this->_conn->_tablePrefix . "core_controller_action AS p 
					ON ru.action_id = p.action_id AND ru.controller_id = p.controller_id AND ru.module_id = p.module_id";
		$rs = $this->_conn->query($sql);
		$rules = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rules[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rules;
	}
	
	public function getRules($objectType, $objectId)
	{
		$sql  = "SELECT CONCAT(ru.object_type, '_', ru.object_id) AS role_name, ru.allow, ru.controller_id AS module_controller, NULL AS action_id
				FROM " . $this->_conn->_tablePrefix . "core_rule AS ru
				WHERE ru.action_id IS NULL AND ru.object_type = \"" . $this->_conn->escape($objectType) . "\" AND ru.object_id = \"" . $this->_conn->escape($objectId) . "\"
				
				UNION 
				
				SELECT CONCAT(ru.object_type, '_', ru.object_id) AS role_name, ru.allow, CONCAT(p.module_id, ':', p.controller_id) AS resource_name_2, p.action_id AS action_name 
				FROM " . $this->_conn->_tablePrefix . "core_rule AS ru
				INNER JOIN " . $this->_conn->_tablePrefix . "core_controller_action AS p 
					ON ru.action_id = p.action_id AND ru.controller_id = p.controller_id AND ru.module_id = p.module_id
				WHERE ru.object_type = \"" . $this->_conn->escape($objectType) . "\" AND ru.object_id = \"" . $this->_conn->escape($objectId) . "\"";
		$rs = $this->_conn->query($sql);
		$rules = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rules[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rules;
	}
	
	public function checkExist($rule)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_rule 
							WHERE module_id = "' . $this->_conn->escape($rule['module_id']) . '"
								AND controller_id = "' . $this->_conn->escape($rule['controller_id']) . '" 
								AND action_id = "' . $this->_conn->escape($rule['action_id']) . '"
								AND object_type = "' . $this->_conn->escape($rule['object_type']) . '"
								AND object_id = "' . $this->_conn->escape($rule['object_id']) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;	
	}
}